﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using MyWebPagesStarterKit.Controls;
using MyWebPagesStarterKit;

public partial class Administration_CssEditor : PageBaseClass
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindData();
    }

    private void BindData()
    {
        gwFiles.DataSource = null;

        List<CssItem> files = new List<CssItem>();
        string path = Server.MapPath("/App_Data/Editor/");
        string css = Server.MapPath(string.Format("/App_Data/Editor/{0}.css", WebSite.GetInstance().Theme));

        if (!Directory.Exists(path))
            Directory.CreateDirectory(path);

        if (!File.Exists(css))
            File.Create(css);
       
        foreach (string file in Directory.GetFiles(path))
        {
            string filename = file.Substring(file.LastIndexOf("\\") + 1);
            CssItem itm = new CssItem(filename, file);
            files.Add(itm);
        }
        files.Sort();
        gwFiles.DataSource = files;
        gwFiles.DataBind();
    }


    protected void Save(object sender, EventArgs e)
    {
        string _path = hFile.Value;
        byte[] data = System.Text.Encoding.Default.GetBytes(cssFile.Text.Trim());
        FileStream fs = new FileStream(_path, FileMode.Create, FileAccess.Write);
        fs.Write(data, 0, data.Length);
        fs.Close();
        cssFile.Text = cssFile.Text.Trim();

    }

    protected void Action(object sender, CommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "tab1":
                multiview.SetActiveView(view1);
                break;
        }
    }

    protected void gwItem_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "edit_item")
        {
            string filename = gwFiles.DataKeys[int.Parse(e.CommandArgument.ToString())].Value.ToString();
            litName.Text = filename;
            filename = Server.MapPath("~/App_Data/Editor/" + filename);
            multiview.SetActiveView(view2);
            t1.Enabled = true;
            hFile.Value = filename;

            try
            {
                StreamReader f = File.OpenText(filename);
                cssFile.Text = f.ReadToEnd();
                f.Close();
            }
            catch
            {
            }
        }
    }



    public class CssItem : IComparable
    {
        private string _Name;
        private string _Created;
        public CssItem(string name, string created)
        {
            this._Name = name;
            this._Created = created;
        }

        public int CompareTo(object obj)
        {
            if (obj is CssItem)
            {
                CssItem p2 = (CssItem)obj;
                return _Name.CompareTo(p2.Name);
            }
            else
                throw new ArgumentException("Object is not a Cssfile.");
        }



        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        public string Created
        {
            get { return _Created; }
            set { _Created = value; }
        }
    }
}
